package com.dongdongshop.service.imp;

import com.alibaba.druid.sql.visitor.functions.If;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.TbItem;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbItemCatSeriveImp implements TbItemCatSerive {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Override
    public List<TbItemCat> getTbItemCat(Long parentId) {
        List<TbItemCat> list = tbItemCatMapper.selectByItemCat(parentId);
        return list;
    }

    @Override
    public List<TbTypeTemplate> selectTemlate() {
        return tbTypeTemplateMapper.selectByTemplate(null);
    }

    @Override
    public int addInfo(TbItemCat tbItemCat) {
        return tbItemCatMapper.insert(tbItemCat);
    }

    @Override
    public TbItemCat toUpdate(Long id) {
        TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(id);
        return tbItemCat;
    }

    @Override
    public int updateInfo(TbItemCat tbItemCat) {
        return tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    @Transactional
    public int deleteInfo(Long[] ids) {
        int i = tbItemCatMapper.deleteInfo(ids);
        List<TbItemCat> list1 = tbItemCatMapper.selectSon(ids);//查询出来parentId
        List<Long> listTwoId = new ArrayList<>();
        if(list1.size() != 0){
            for (TbItemCat tb:list1) {
                if(tb != null){
                    listTwoId.add(tb.getId());
                    tbItemCatMapper.deleteByPrimaryKey(tb.getId());
                }
            }
        }
        for (Long paretTwe:listTwoId) {
             List<TbItemCat> tbItemCat = tbItemCatMapper.selectParentId(paretTwe);
            if(tbItemCat != null){
                for (TbItemCat tb: tbItemCat) {
                    int i1 = tbItemCatMapper.deleteByPrimaryKey(tb.getId());
                }
            }
        }

        return 1;
    }

    @Override
    public TbItemCat showTypeId(Long parentId) {
        return tbItemCatMapper.selectByPrimaryKey(parentId);
    }

    @Override
    public List<TbItemCat> showItem() {
        return tbItemCatMapper.selectByExample(null);
    }
}
